using System.Data;
using System.Data.SqlClient;
using System.Web.Services;
using System.Web;
using System.Xml;
using System;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington
	Started:	2004-08-10
	Status:		live
	Version:	1.0.2
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/
	
namespace umlungu.engineX.workflowX {
	/// <summary>
	/// This class gives access to a single work-process or list of work-processes defined in the datastore.
	/// <para>A work-process defines a particular workflow as a series of 'tasks'.</para>
	/// <para>Each task is defined by the agent, the expected duration, a description and the action required.</para>
	/// <para>Typically the first and last tasks of a workflow are assigned to the 'System' agent</para>
	/// </summary>
	public class x_workprocess : x_DbData {
		#region Flags
		private bool wantGKAgent = false;
		#endregion

		#region Constants
		private const string workprocessxml = "<workflowX><workprocess/></workflowX>";
		private const string error_get 			= "The work-process does not exist within the system.";
		private const string error_nullid		= "The work-process identifier is null.";
		private const string error_xml			= "The work-process has invalid process xml.";
		private const string error_agent		= "The agent identifier is null.";
		#endregion

		#region Invisible properties
		private XmlDocument workprocess;
		#endregion

		#region Visible properties
		/// <summary>A work-process definiton</summary>
		/// <value>The work-process definiton rendered as an Xml Element</value>
		public XmlElement WorkProcess {
			get { return (XmlElement) workprocess.SelectSingleNode("/workflowX/workprocess"); }
		}
		/// <summary>A work-process collection</summary>
		/// <value>The collection of work-processes rendered as an Xml Element</value>
		public XmlElement Processes {
			get { 
				getProcesses();
				return (XmlElement) workprocess.SelectSingleNode("/workflowX/workprocess/workprocesses");
			}
		}
		private int id;
		/// <summary>The work-process identifier</summary>
		/// <value>The work-process identifier within the datastore</value>
		public int ID {
			get { return id; }
			set { 
				id = value;
				WorkProcess.SetAttribute("id", id.ToString());
			}
		}
		#endregion

		#region Constructors/Destructors
		/// <overloads>work-process constructor</overloads>
		/// <summary>Initialises the datastore connection string</summary>
		/// <param name="DSN">the dsn (from web.config)</param>
		public x_workprocess(string DSN) : base(DSN) {
			initialise();
		}
		/// <summary>Retrieves the work-process from the datastore</summary>
		/// <param name="DSN">the dsn (from web.config)</param>
		/// <param name="ProcessID">The work-process identifier</param>
		/// <exception cref="x_exception">Cannot find a work-process for the given identifier</exception>
		public x_workprocess(string DSN, int ProcessID) : base(DSN) {
			initialise();
			Get(ProcessID);
		}
		#endregion

		#region Public methods
		///	<summary>
		///	Creates a work-process and saves it into the database
		///	</summary>
		/// <param name="Name">The name of the work-process</param>
		/// <param name="Description">A description of the work-process</param>
		/// <param name="Duration">The expected duration (in hours) of the work-process</param>
		/// <param name="ProcessXml">The Xml definition of the work-process.</param>
		/// <param name="OwnerID">The work-process owner identifier</param>
		public void Create(string Name, string Description, int Duration, string ProcessXml, int OwnerID) {
			_getCommand("xProcessAdd", CommandType.StoredProcedure);
			_addDbParameter(Name, "@Name", 50);
			_addDbParameter(Description, "@Description", 200);
			_addDbParameter(Duration, "@Duration");
			try {
				XmlDocument procdoc = new XmlDocument();
				procdoc.LoadXml(ProcessXml);
			} catch(XmlException) {
				throw(new x_exception("error_xml", error_xml));
			}
			_addDbParameter(ProcessXml, "@Xml", 8000);
			_addDbParameter(OwnerID, "@OwnerID");
			IDataParameter parID = _addDbParameter("@ID");
			_executeNonQuery();
			ID = (int) _getParameter(parID);
		}
		/// <overloads>Retreives a work process from the database</overloads>
		///	<summary>
		///	Process is specified
		///	</summary>
		/// <param name="ProcessID"></param>
		public void Get(int ProcessID) {
			ID = ProcessID;
			Get();
		}
		///	<summary>
		///	Process identifier has been set
		///	</summary>
		public void Get() {
			if (id == 0)
				throw(new x_exception("error_nullid", error_nullid));
			else {
				_getCommand("xProcessGet", CommandType.StoredProcedure);
				_addDbParameter(ID, "@ID");
				_addDbParameter(0, "@OwnerID");
				SqlDataReader rdr = (SqlDataReader) _executeReader(CommandBehavior.Default);
				if(rdr.Read()) {
					_addAttribute(workprocess, WorkProcess, "name", _getString(rdr, "Name"));
					_addAttribute(workprocess, WorkProcess, "owner", _getString(rdr, "OwnerID"));
					_addAttribute(workprocess, WorkProcess, "activateID", _getString(rdr, "ActivateID"));
					_addElement(workprocess, WorkProcess, "description", _getString(rdr, "Description"));
					_addElement(workprocess, WorkProcess, "duration", _getString(rdr, "Duration"));
					_addElement(workprocess, WorkProcess, "activation", _getString(rdr, "Activate"));
					string procxml = _getString(rdr, "XML");
					_addElement(workprocess, WorkProcess, "process", "");
					WorkProcess.SelectSingleNode("process").InnerXml = procxml;
				} else {
					throw(new x_exception("error_get", error_get));
				}
				rdr.Close();
			}
		}
		/// <overloads>Get a list of processes (different format to Processes property)</overloads>
		/// <summary>
		/// Processes for all owners
		/// </summary>
		public void GetProcess() {
			GetProcess(0);
		}
		/// <summary>
		/// Processes for an specified owner
		/// </summary>
		public void GetProcess(int OwnerID) {
			_getCommand("xProcessGet", CommandType.StoredProcedure);
			_addDbParameter(0, "@ID");
			_addDbParameter(OwnerID, "@OwnerID");
			SqlDataReader rdr = (SqlDataReader) _executeReader(CommandBehavior.Default);
			XmlElement processes = (XmlElement) WorkProcess.AppendChild(workprocess.CreateElement("processes"));
			while(rdr.Read()) {
				XmlElement process = (XmlElement) processes.AppendChild(workprocess.CreateElement("process"));
				_addAttribute(workprocess, process, "ID", _getString(rdr, "ID"));
				_addAttribute(workprocess, process, "name", _getString(rdr, "Name"));
				process.InnerText = _getString(rdr, "Description");
			}
			rdr.Close();
		}
		/// <summary>
		/// Processes for an specified owner
		/// </summary>
		/// <param name="OwnerID">The work-process owner identifier</param>
		public void GetProcesses(int OwnerID) {
			getProcesses(OwnerID);
		}
		/// <summary>
		/// The process for a specified agent
		/// </summary>
		/// <param name="AgentID">The agent assigned to work-process</param>
		/// <param name="ForAgent">Ovderload purpose only - No effect</param>
		public void GetProcess(int AgentID, bool ForAgent) {
			_getCommand("xAgentProcessGet", CommandType.StoredProcedure);
			_addDbParameter(AgentID, "@AgentID");
			SqlDataReader rdr = (SqlDataReader) _executeReader(CommandBehavior.Default);
			XmlElement processes = (XmlElement) WorkProcess.AppendChild(workprocess.CreateElement("workprocesses"));
			while(rdr.Read()) {
				XmlElement process = (XmlElement) processes.AppendChild(workprocess.CreateElement("workprocess"));
				_addAttribute(workprocess, process, "id", _getString(rdr, "ID"));
				_addAttribute(workprocess, process, "owner", _getString(rdr, "OwnerID"));
				_addAttribute(workprocess, process, "name", _getString(rdr, "Name"));
				_addAttribute(workprocess, process, "activateID", _getString(rdr, "ActivateID"));
				_addElement(workprocess, process, "description", _getString(rdr, "Description"));
				_addElement(workprocess, process, "duration", _getString(rdr, "Duration"));
				_addElement(workprocess, process, "activation", _getString(rdr, "Activate"));
			}
			rdr.Close();
		}
		/// <summary>
		/// Processes for all owners
		/// </summary>
		public void GetProcesses() {
			getProcesses(0);
		}
		private void getProcesses() {
			getProcesses(0);
		}
		private void getProcesses(int OwnerID) {
			_getCommand("xProcessGet", CommandType.StoredProcedure);
			_addDbParameter(0, "@ID");
			_addDbParameter(OwnerID, "@OwnerID");
			SqlDataReader rdr = (SqlDataReader) _executeReader(CommandBehavior.Default);
			XmlElement processes = (XmlElement) WorkProcess.AppendChild(workprocess.CreateElement("workprocesses"));
			while(rdr.Read()) {
				XmlElement process = (XmlElement) processes.AppendChild(workprocess.CreateElement("workprocess"));
				_addAttribute(workprocess, process, "id",                   _getString(rdr, "ID"));
				_addAttribute(workprocess, process, "owner",                _getString(rdr, "OwnerID"));
                _addAttribute(workprocess, process, "name",                 _getString(rdr, "Name"));
                _addAttribute(workprocess, process, "activateID",           _getString(rdr, "ActivateID"));
                _addAttribute(workprocess, process, "proc_typeID",          _getString(rdr, "ProcessTypeID"));
                _addAttribute(workprocess, process, "proc_type_categoryID", _getString(rdr, "ProcessTypeCategoryID"));
				_addElement(workprocess, process, "description",        _getString(rdr, "Description"));
				_addElement(workprocess, process, "duration",           _getString(rdr, "Duration"));
                _addElement(workprocess, process, "activation",         _getString(rdr, "Activate"));
                _addElement(workprocess, process, "proc_type",          _getString(rdr, "ProcessType"));
                _addElement(workprocess, process, "proc_type_category", _getString(rdr, "ProcessTypeCategory"));
			}
			rdr.Close();
		}
		///	<summary>
		///	Saves an existing work-process to the database
		///	</summary>
		public void Save(int thisid, string Name, string Description, int Duration, string Xml, int OwnerID, int ProcessTypeID) {
			ID = thisid;
			_getCommand("xProcessSave", CommandType.StoredProcedure);
			_addDbParameter(ID, "@ID");
			_addDbParameter(Name, "@Name", 50);
			_addDbParameter(Description, "@Description", 200);
			_addDbParameter(Duration, "@Duration");
			_addDbParameter(Xml, "@Xml", 8000);
			_addDbParameter(OwnerID, "@OwnerID");
			_addDbParameter(ProcessTypeID, "@ProcessTypeID");
            _addDbParameter(Description, "@ProcessTypeDescription", 100);
			_executeNonQuery();
		}
		/// <overloads>Gets a list of agents for the work-process</overloads>
		/// <summary>
		/// Gets agents for the current process
		/// </summary>
		/// <exception cref="x_exception">error_nullid: <para>The work-process identifier is null.</para></exception>
		public void GetAgents() {
			Get();
			getAgents();
        }
        /// <summary>
        /// Gets agents for the specified process
        /// </summary>
        /// <param name="processID">The work-process identifier</param>
        public void GetAgents(int processID){
            ID = processID;
            GetAgents();
        }
        /// <summary>
        /// Gets agents for the specified process
        /// </summary>
        /// <param name="processID">The work-process identifier</param>
        public void GetAgents(string processID){
            ID = Convert.ToInt32(processID);
            GetAgents();
        }
        /// <summary>
        /// Gets agents for the specified process and owner
        /// </summary>
        /// <param name="processID">The work-process identifier</param>
        /// <param name="ownerID">The identifier of the work-process owner</param>
        public void GetAgents(int processID, int ownerID) {
            ID = processID;
            Get();
            getAgents(ownerID, 0);
        }
        /// <summary>
        /// Gets agents for the specified process and owner
        /// </summary>
        /// <param name="ownerID">The identifier of the work-process owner</param>
        /// <param name="stateID">The identifier of the work-process state</param>
        /// <param name="excludeTaskStateID">The identifier of task state of tasks assigned to agent</param>
        public void GetAgentsCheckTasks(int ownerID, int stateID, int excludeTaskStateID) {
            ID = 0;
            getAgents(ownerID, stateID, excludeTaskStateID);
        }
		/// <summary>
		/// Gets agents for the specified process, owner and agent-state
		/// </summary>
		/// <param name="processID">The work-process identifier</param>
		/// <param name="ownerID">The identifier of the work-process owner</param>
		/// <param name="agentStateID">The identifier of the agent state</param>
		public void GetAgents(int processID, int ownerID, int agentStateID) {
			ID = processID;
            if(ID!=0)
			    Get();
			getAgents(ownerID, agentStateID);
		}
		private void getAgents() {
			getAgents(0, 0);
        }
        private void getAgents(int ownerID, int agentStateID) {
           getAgents(ownerID, agentStateID, 0);
        }
        private void getAgents(int ownerID, int agentStateID, int taskStateID) {
            x_workflow workflowx = new x_workflow();
            _getCommand("xAgentsGet", CommandType.StoredProcedure);
            _addDbParameter(ID, "@ProcessID");
            _addDbParameter(agentStateID, "@AgentStateID");
            _addDbParameter(ownerID, "@OwnerID");
            _addDbParameter(taskStateID, "@TaskStateID");
            using (SqlDataReader rdr = (SqlDataReader)_executeReader(CommandBehavior.Default)) {
                XmlElement agents = (XmlElement)WorkProcess.AppendChild(workprocess.CreateElement("agents"));
                while (rdr.Read()) {
                    XmlElement agent = (XmlElement)agents.AppendChild(workprocess.CreateElement("agent"));
                    string agentid = _getString(rdr, "AgentID");
                    _addAttribute(workprocess, agent, "ID", agentid);
                    try {
                        if (wantGKAgent)
                            getAgent(agentid, agent, workflowx);
                        else
                            getAgent(agentid, agent, rdr);
                    } catch (x_exception e) {
                        _addAttribute(workprocess, agent, "email", "unknown@nashuamobile.com");
                        //agent.InnerText = "[unknown]";
                        agent.InnerText = e.Message;
                    }
                }
                rdr.Close();
            }
        }
		private void getAgent(string agntid, XmlElement agent, SqlDataReader rdr) {
			_addElement(workprocess, agent, "State", _getString(rdr, "AgentState"), "StateID", _getString(rdr, "AgentStateID"));
			_addElement(workprocess, agent, "Profile", _getString(rdr, "AgentProfile"), "ProfileID", _getString(rdr, "AgentProfileID"));
			_addElement(workprocess, agent, "Process", _getString(rdr, "Process"), "ProcessID", _getString(rdr, "ProcessID"));
			_addElement(workprocess, agent, "Firstname", _getString(rdr, "FirstName"));
			_addElement(workprocess, agent, "Surname", _getString(rdr, "Surname"));
			_addElement(workprocess, agent, "Name", String.Concat(_getString(rdr, "FirstName"), " ", _getString(rdr, "Surname")));
			_addElement(workprocess, agent, "Cellphone", _getString(rdr, "CellPhone"));
			_addElement(workprocess, agent, "Email", _getString(rdr, "Email"));
		}
		private void getAgent(string agntid, XmlElement agent, x_workflow wkflowx) {
			XmlElement gkAgent = wkflowx.GetUser(agntid);
			_addAttribute(workprocess, agent, "email", gkAgent.SelectSingleNode("EMail").InnerText);
			agent.InnerText = String.Concat(gkAgent.SelectSingleNode("FirstName").InnerText, " ", gkAgent.SelectSingleNode("Surname").InnerText);
		}
		/// <summary>
		/// Gets an agent
		/// </summary>
		/// <param name="AgentID">The work-process identifier</param>
		public XmlElement GetAgentXml(int AgentID) {
			_getCommand("xAgentGet", CommandType.StoredProcedure);
			_addDbParameter(AgentID, "@AgentID");
			XmlElement agent = null;
			using (SqlDataReader rdr = (SqlDataReader) _executeReader(CommandBehavior.Default)) {
				if (rdr.Read()) {
					agent = (XmlElement) WorkProcess.AppendChild(workprocess.CreateElement("agent"));
					string agentid = _getString(rdr, "AgentID");
					_addAttribute(workprocess, agent, "ID", agentid);
					try {
						getAgent(agentid, agent, rdr);
					} catch(x_exception e) {
						_addAttribute(workprocess, agent, "email", "unknown@nashuamobile.com");
						agent.InnerText = e.Message;
					}
				}
				rdr.Close();
			}
			return agent;
		}
		/// <summary>
		/// Gets an agent
		/// </summary>
		/// <param name="AgentID">The work-process identifier</param>
		public void GetAgent(int AgentID) {
			getAgent(AgentID);
		}
		private void getAgent(int agentID) {
			x_workflow workflowx = new x_workflow();
			_getCommand("xAgentGet", CommandType.StoredProcedure);
			_addDbParameter(agentID, "@AgentID");
			using (SqlDataReader rdr = (SqlDataReader) _executeReader(CommandBehavior.Default)) {
				if (rdr.Read()) {
					XmlElement agent = (XmlElement) WorkProcess.AppendChild(workprocess.CreateElement("agent"));
					string agentid = _getString(rdr, "AgentID");
					_addAttribute(workprocess, agent, "ID", agentid);
					try {
						if (wantGKAgent)
							getAgent(agentid, agent, workflowx);
						else
							getAgent(agentid, agent, rdr);
					} catch(x_exception e) {
						_addAttribute(workprocess, agent, "email", "unknown@nashuamobile.com");
						//agent.InnerText = "[unknown]";
						agent.InnerText = e.Message;
					}
				}
				rdr.Close();
			}
		}
		/// <overloads>Adds an agent to the list of agents for the work-process</overloads>
		/// <summary>
		/// Adds an agent to the current process
		/// </summary>
		/// <param name="agentID">The agent identifier</param>
		/// <param name="firstname">The Agent's first name</param>
		/// <param name="surname">The Agent's surname</param>
		/// <param name="email">The Agent's email</param>
		/// <param name="cellphone">The Agent's cell number</param>
		/// <exception cref="x_exception">error_nullid: <para>The work-process identifier is null.</para></exception>
		/// <exception cref="x_exception">error_agent: <para>The agent identifier is null.</para></exception>
		public void AddAgent(int agentID, string firstname, string surname, string email, string cellphone) {
			if (id == 0)
				throw(new x_exception("error_nullid", error_nullid));
			else if (agentID == 0)
				throw(new x_exception("error_agent", error_agent));
			else
				addAgent(agentID, firstname, surname, email, cellphone);
		}
		/// <summary>
		/// Adds an agent to the specified process - process ID can be 0, agent id cannot be 0
		/// </summary>
		/// <param name="processID">The work-process identifier</param>
		/// <param name="agentID">The agent identifier</param>
		/// <param name="firstname">The Agent's first name</param>
		/// <param name="surname">The Agent's surname</param>
		/// <param name="email">The Agent's email</param>
		/// <param name="cellphone">The Agent's cell number</param>
		public void AddAgent(int processID, int agentID, string firstname, string surname, string email, string cellphone) {
			ID = processID;
			if (agentID == 0)
				throw(new x_exception("error_agent", error_agent));
			else
				addAgent(agentID, firstname, surname, email, cellphone);
		}
		/// <summary>
		/// Adds an agent to the specified process - process ID cannot be 0, agent id cannot be 0
		/// </summary>
		/// <param name="processID">The work-process identifier</param>
		/// <param name="agentID">The agent identifier</param>
		/// <param name="firstname">The Agent's first name</param>
		/// <param name="surname">The Agent's surname</param>
		/// <param name="email">The Agent's email</param>
		/// <param name="cellphone">The Agent's cell number</param>
		public void AddAgent(string processID, string agentID, string firstname, string surname, string email, string cellphone) {
			ID = Convert.ToInt32(processID);
			int AgntID = Convert.ToInt32(agentID);
			if (AgntID == 0)
				throw(new x_exception("error_agent", error_agent));
			AddAgent(AgntID, firstname, surname, email, cellphone);
		}
		private void addAgent(int agentID, string firstname, string surname, string email, string cellphone) {
			_getCommand("xAgentAdd", CommandType.StoredProcedure);
			_addDbParameter(ID, "@ProcessID");
			_addDbParameter(agentID, "@AgentID");
			_addDbParameter(firstname, "@FirstName", 50);
			_addDbParameter(surname, "@Surname", 50);
			_addDbParameter(email, "@EMail", 50);
			_addDbParameter(cellphone, "@CellPhone", 20);
			_executeNonQuery();
		}
		/// <overloads>Deletes an agent from the list of agents for the work-process</overloads>
		/// <summary>
		/// Deletes an agent from the current process
		/// </summary>
		/// <param name="agentID">The agent identifier</param>
		/// <exception cref="x_exception">error_nullid: <para>The work-process identifier is null.</para></exception>
		/// <exception cref="x_exception">error_agent: <para>The agent identifier is null.</para></exception>
		public void DeleteAgent(int agentID) {
			if (id == 0)
				throw(new x_exception("error_nullid", error_nullid));
			else if (agentID == 0)
				throw(new x_exception("error_agent", error_agent));
			else
				deleteAgent(agentID);
		}
		/// <summary>
		/// Deletes an agent from the specified process
		/// </summary>
		/// <param name="agentID">The agent identifier</param>
		/// <param name="processID">The work-process identifier</param>
		public void DeleteAgent(int processID, int agentID) {
			ID = processID;
			DeleteAgent(agentID);
		}
		/// <summary>
		/// Deletes an agent from the specified process
		/// </summary>
		/// <param name="agentID">The agent identifier</param>
		/// <param name="processID">The work-process identifier</param>
		public void DeleteAgent(string processID, string agentID) {
			ID = Convert.ToInt32(processID);
			DeleteAgent(Convert.ToInt32(agentID));
		}
		private void deleteAgent(int agentID) {
			_getCommand("xAgentDelete", CommandType.StoredProcedure);
			_addDbParameter(ID, "@ProcessID");
			_addDbParameter(agentID, "@AgentID");
			_executeNonQuery();
		}
		/// <overloads>Updates the state of an agent</overloads>
		/// <summary>
		/// Updates the state of an agent
		/// </summary>
		/// <param name="agentID">The agent identifier</param>
		/// <param name="stateID">The agent state identifier</param>
		public void UpdateState(string agentID, string stateID) {
			UpdateState(Convert.ToInt32(agentID), Convert.ToInt32(stateID));
		}
		/// <param name="agentID">The agent identifier</param>
		/// <param name="stateID">The agent state identifier</param>
		public void UpdateState(int agentID, int stateID) {
			_getCommand("xAgentState", CommandType.StoredProcedure);
			_addDbParameter(agentID, "@AgentID");
			_addDbParameter(stateID, "@StateID");
			_executeNonQuery();
		}
		/// <overloads>Assigns the agent to a new process</overloads>
		/// <summary>
		/// Assigns the agent to a new process
		/// </summary>
		/// <param name="agentID">The agent identifier</param>
		/// <param name="oldProcessID">The agent process identifier (old)</param>
		/// <param name="newProcessID">The agent process identifier (new)</param>
		public void UpdateProcess(string agentID, string oldProcessID, string newProcessID) {
			UpdateProcess(Convert.ToInt32(agentID), Convert.ToInt32(oldProcessID), Convert.ToInt32(newProcessID));
		}
		/// <param name="agentID">The agent identifier</param>
		/// <param name="oldProcessID">The agent process identifier (old)</param>
		/// <param name="newProcessID">The agent process identifier (new)</param>
		public void UpdateProcess(int agentID, int oldProcessID, int newProcessID) {
			_getCommand("xAgentProcessDelete", CommandType.StoredProcedure);
			_addDbParameter(agentID, "@AgentID");
			_addDbParameter(oldProcessID, "@ProcessID");
			_executeNonQuery();
			if (newProcessID > 0) {
				_getCommand("xAgentProcessAdd", CommandType.StoredProcedure);
				_addDbParameter(agentID, "@AgentID");
				_addDbParameter(newProcessID, "@ProcessID");
				_executeNonQuery();
			}
		}
		/// <overloads>Removes the agent from a process</overloads>
		/// <summary>
		/// Removes the agent from a process
		/// </summary>
		/// <param name="agentID">The agent identifier</param>
		/// <param name="processID">The agent process identifier (old)</param>
		public void Remove(string agentID, string processID) {
			UpdateProcess(Convert.ToInt32(agentID), Convert.ToInt32(processID), 0);
		}
		/// <param name="agentID">The agent identifier</param>
		/// <param name="processID">The agent process identifier (old)</param>
		public void Remove(int agentID, int processID) {
			UpdateProcess(agentID, processID, 0);
		}
		/// <overloads>Updates the activation of a process</overloads>
		/// <summary>
		/// Updates the activation of a process
		/// </summary>
		/// <param name="processID">The process identifier</param>
		/// <param name="activateID">The activation identifier</param>
		public void UpdateActivate(string processID, string activateID) {
			UpdateState(Convert.ToInt32(processID), Convert.ToInt32(activateID));
		}
		/// <param name="processID">The process identifier</param>
		/// <param name="activateID">The activation identifier</param>
		public void UpdateActivate(int processID, int activateID) {
			ID = processID;
			_getCommand("xProcessActivate", CommandType.StoredProcedure);
			_addDbParameter(ID, "@ID");
			_addDbParameter(activateID, "@ActivateID");
			_executeNonQuery();
			Get();
		}
		/// <overloads>Gets the states for agents</overloads>
		public void GetAgentStates() {
			GetAgentStates(0);
		}
		/// <param name="stateID">The agent-state identifier, or 0 for all</param>
		public void GetAgentStates(string stateID) {
			GetAgentStates(Convert.ToInt32(stateID));
        }
        /// <param name="stateID">The agent-state identifier, or 0 for all</param>
        public void GetAgentStates(int stateID) {
            _getCommand("xAgentStateGet", CommandType.StoredProcedure);
            _addDbParameter(stateID, "@ID");
            WorkProcess.SetAttribute("id", stateID.ToString());
            using (SqlDataReader rdr = (SqlDataReader)_executeReader(CommandBehavior.Default)) {
                XmlElement states = (XmlElement)WorkProcess.AppendChild(workprocess.CreateElement("agentstates"));
                while (rdr.Read()) {
                    XmlElement state = (XmlElement)states.AppendChild(workprocess.CreateElement("agentstate"));
                    _addAttribute(workprocess, state, "id", _getString(rdr, "ID"));
                    _addAttribute(workprocess, state, "name", _getString(rdr, "Name"));
                    _addElement(workprocess, state, "description", _getString(rdr, "Description"));
                }
                rdr.Close();
            }
        }

		/// <overloads>Sets the agent index for the process</overloads>
		/// <param name="processID">The process identifier</param>
		/// <param name="index">The process index</param>
		public void SetIndex(int processID, int index) {
			ID = processID;
			SetIndex(index);
		}
		/// <param name="index">The process index</param>
		public void SetIndex(int index) {
			_getCommand("xProcessSetIndex", CommandType.StoredProcedure);
			_addDbParameter(ID, "@ID");
			_addDbParameter(index, "@Index");
			_executeNonQuery();
		}
		/// <overloads>Gets the agent index for the process</overloads>
		public int GetIndex() {
			int thisindex = 0;
			_getCommand("xProcessGetIndex", CommandType.StoredProcedure);
			_addDbParameter(ID, "@ID");
			using (SqlDataReader rdr = (SqlDataReader) _executeReader(CommandBehavior.Default)) {
				if (rdr.Read()) {
					thisindex = Convert.ToInt32(_getString(rdr, "Index"));
				}
				rdr.Close();
			}
			return thisindex;
		}
		/// <param name="processID">The process identifier</param>
		public int GetIndex(int processID) {
			ID = processID;
			return GetIndex();
		}
		#endregion
		
		#region Protected methods
		#endregion
				
		#region Private methods
		private void initialise() {
			workprocess = new XmlDocument();
			workprocess.LoadXml(workprocessxml);
		}
		#endregion

	}
}
